import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.channels.SocketChannel;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import static java.lang.System.out;

public class ChatWindow extends JFrame implements ActionListener
{
    String toUserName;

    JTextArea receivedMessagesArea;

    JTextArea msgJtextArea;

    JButton btnSend, btnSendAll;

    SocketChannel client = ChatClientUI.client;

    String fromUserName = ChatClientUI.currentUserName;

    Map<String, ChatWindow> userChatWindow = ChatClientUI.userChatWindow;

    public ChatWindow(String toUserName) throws Exception
    {
        super(toUserName); // set title for frame
        this.toUserName = toUserName;
        showChatWindow();
    }

    public void showChatWindow()
    {
        btnSend = new JButton("Send");
        btnSendAll = new JButton("Send All");
        receivedMessagesArea = new JTextArea();
        receivedMessagesArea.setRows(10);
        receivedMessagesArea.setColumns(50);
        receivedMessagesArea.setEditable(false);
        msgJtextArea = new JTextArea();
        msgJtextArea.setLineWrap(true);
        JScrollPane messageTextAreaPane = new JScrollPane(msgJtextArea,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        messageTextAreaPane.setPreferredSize(new Dimension(400, 50));

        receivedMessagesArea.setLineWrap(true);
        JScrollPane sp = new JScrollPane(receivedMessagesArea,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        add(sp, "Center");
        JPanel bp = new JPanel(new FlowLayout());
        bp.add(messageTextAreaPane);
        bp.add(btnSend);
        bp.add(btnSendAll);
        add(bp, "South");
        btnSend.addActionListener(this);
        btnSendAll.addActionListener(this);
        setSize(500, 300);
        setVisible(true);
        pack();
        attachCloseListener();

    }

    public void attachCloseListener()
    {
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                userChatWindow.remove(toUserName);
                dispose();
            }
        });
    }

    public void actionPerformed(ActionEvent evt)
    {
        if (evt.getSource() == btnSendAll)
        {
            sendMessage(RequestActionCodes.SENDALL_ACTION_CODE);
        }
        else
        {
            sendMessage(RequestActionCodes.SEND_ACTION_CODE);
        }
    }

    public void sendMessage(String action)
    {
        String message = msgJtextArea.getText();
        if (message != null && !message.trim().equals(""))
        {
            setMessages(" " + fromUserName + " : " + message + "\n");
            new MessageSender(StringUtils.getFormattedString(action, fromUserName, toUserName,
                    msgJtextArea.getText()));
        }
    }
    public void setMessages(String message){
    receivedMessagesArea.append(message + "\n");
    }
} // end of client
